package Q_Learning;

import javax.swing.*;

import java.awt.*;

public class QFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel alphaLabel;
	private JLabel randomLabel;
	private JPanel controlPanel;
	private JTextField gammaText;
	private JPanel showPanel;
	private JPanel logPanel;;
	private GridPanel[][] grid;
	private JTextField randomText;
	private JLabel gammaLabel;
	private JTextField alphaText;
	private JButton runButton;
	private JButton learnButton;
	private JButton resetButton;
	private JLabel stepLabel;
	private JTextField stepText;
	private JTextArea logArea;
	
	public JButton getButton(){
		return runButton;
	}
	public double getParameter( int type ){
		double ret;
		switch(type){
		case 0:
			ret = Double.parseDouble(alphaText.getText());
			break;
		case 1:
			ret = Double.parseDouble(gammaText.getText());
			break;
		case 2:
			ret = Double.parseDouble(randomText.getText());
			break;
		default:
			ret = Double.NEGATIVE_INFINITY;
		}
		return ret;
	}
	public Integer getStep( int type ){
		if ( type == 3 ){
			return Integer.parseInt(stepText.getText());
		}else {
			return Integer.MIN_VALUE;
		}
	}
	
	public void setlog( String s ){
		logArea.append(s);
	}
	public void resetlog(){
		logArea.setText("");
	}
	public QFrame() {
		super();
		
		initGUI();
		runButton.addActionListener(new RunListener());
		learnButton.addActionListener(new LearnListener());
		resetButton.addActionListener(new ResetListener());
	}
	public GridPanel[][] getGrids(){
		return grid;
	}
	private void initGUI() {
		Container c = getContentPane();
		grid = new GridPanel[GridWorld.xNum][GridWorld.yNum];
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints cns = new GridBagConstraints();
		GridLayout gLayout = new GridLayout(GridWorld.xNum,GridWorld.yNum,10,10);
		JPanel controlPanel = new JPanel();
		JPanel showPanel = new JPanel();
		JPanel logPanel = new JPanel();
		
		
		controlPanel.setBorder(BorderFactory.createTitledBorder("Control Panel"));
		controlPanel.setLayout(layout);
		
		alphaLabel = new JLabel("Alpha");
		cns.fill = GridBagConstraints.HORIZONTAL;
		cns.gridx = 0;
		cns.gridy = 0;
		controlPanel.add(alphaLabel, cns);
		
		gammaLabel = new JLabel("Gamma");
		cns.fill = GridBagConstraints.HORIZONTAL;
		cns.gridx = 0;
		cns.gridy = 1;
		controlPanel.add(gammaLabel, cns);
		
		randomLabel = new JLabel("Random");
		cns.fill = GridBagConstraints.HORIZONTAL;
		cns.gridx = 0;
		cns.gridy = 2;
		controlPanel.add(randomLabel, cns);
		
		alphaText = new JTextField("0.5");
		cns.fill = GridBagConstraints.HORIZONTAL;
		cns.gridx = 1;
		cns.gridy = 0;
		controlPanel.add(alphaText, cns);
		
		gammaText = new JTextField("0.5");
		cns.fill = GridBagConstraints.HORIZONTAL;
		cns.gridx = 1;
		cns.gridy = 1;
		controlPanel.add(gammaText, cns);
		
		randomText = new JTextField("0.8");
		cns.fill = GridBagConstraints.HORIZONTAL;
		cns.gridx = 1;
		cns.gridy = 2;
		controlPanel.add(randomText, cns);
		
		stepLabel = new JLabel("Step");
		cns.fill = GridBagConstraints.HORIZONTAL;
		cns.gridx = 2;
		cns.gridy = 0;
		controlPanel.add(stepLabel, cns);
		
		stepText = new JTextField("1000");
		cns.fill = GridBagConstraints.HORIZONTAL;
		cns.gridx = 3;
		cns.gridy = 0;
		controlPanel.add(stepText, cns);
		
		resetButton = new JButton("reset");
		cns.fill = GridBagConstraints.HORIZONTAL;
		cns.gridx = 4;
		cns.gridy = 2;
		controlPanel.add(resetButton, cns);
		
		learnButton = new JButton("learn");
		cns.fill = GridBagConstraints.HORIZONTAL;
		cns.gridx = 3;
		cns.gridy = 2;
		controlPanel.add(learnButton, cns);
		
		runButton = new JButton("go");
		cns.fill = GridBagConstraints.HORIZONTAL;
		cns.gridx = 2;
		cns.gridy = 2;
		controlPanel.add(runButton, cns);
		
		c.add(controlPanel, BorderLayout.NORTH);
		
		showPanel.setBorder(BorderFactory.createTitledBorder("Show Panel"));
		showPanel.setLayout(gLayout);
		for ( int i=0 ; i < GridWorld.xNum ; i++ ){
			for ( int j=0 ; j < GridWorld.yNum ; j++ ) {
				grid[i][j] = new GridPanel( i, j );
				showPanel.add(grid[i][j]);
			}
		}
		c.add(showPanel, BorderLayout.CENTER);
		
		logPanel.setBorder(BorderFactory.createTitledBorder("Log Panel"));
		logPanel.setLayout(new BorderLayout());
		logArea = new JTextArea();
		logArea.setEditable(false);
		logPanel.add(logArea, BorderLayout.CENTER);
		c.add(logPanel, BorderLayout.SOUTH);
	}
	public void setControlPanel(JPanel controlPanel) {
		this.controlPanel = controlPanel;
	}
	public JPanel getControlPanel() {
		return controlPanel;
	}
	public void setShowPanel(JPanel showPanel) {
		this.showPanel = showPanel;
	}
	public JPanel getShowPanel() {
		return showPanel;
	}
	
}
